<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require_once 'sitecontroller.php';

class Procedimento extends SiteController {
    
    public function add($idFluxo){
        $this->load->library('form_validation');
        $this->form_validation->set_rules('descricao', 'Descri&ccedil;&atilde;o', 'trim|required');
        
        $fluxo = $this->doctrine->em->getRepository('Entities\Fluxo')->find($idFluxo);
        
        if ($this->form_validation->run()) {
            $procedimento = new Entities\Procedimento();
            $procedimento->setDescricao($this->input->post('descricao'));
            $procedimento->setFluxo($fluxo);
            
            $this->doctrine->em->persist($procedimento);            
            $this->doctrine->em->flush();
            
            if ($this->input->post('ics') != ''){
                $icsStr = explode(',',$this->input->post('ics'));
                foreach($icsStr as $icStr){
                    $ic = $this->doctrine->em->getRepository('Entities\Item')->find($icStr*1);
                    if(!is_null($ic)){
                        $procxic = new Entities\Procedimentoic();
                        $procxic->setIc($ic);
                        $procxic->setProcedimento($procedimento);

                        $this->doctrine->em->persist($procxic);            
                        $this->doctrine->em->flush();
                    };
                }
            }
        }
        
        redirect('/chamado/ver/'.$fluxo->getChamado()->getId());
    }
    
}

